package io.github.qifan777.server.config;

import io.github.qifan777.server.infrastructure.utils.JsonUtils;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
@AllArgsConstructor
public class ConfigRepository {
    private StringRedisTemplate redisTemplate;

    public Config find() {
        return Optional.ofNullable(redisTemplate.opsForValue().get("config"))
                .map(value -> JsonUtils.fromJson(value, Config.class))
                .orElse(new Config());
    }

    public void save(Config config) {
        redisTemplate.opsForValue().set("config", JsonUtils.toJson(config));
    }
}
